package com.atguigu.guli.edu.service.impl;

import com.atguigu.common_utils.utils.PageUtils;
import com.atguigu.common_utils.utils.Query;
import com.atguigu.guli.edu.entity.Teacher;
import com.atguigu.guli.edu.entity.query.TeacherQuery;
import com.atguigu.guli.edu.feign.OssFeign;
import com.atguigu.guli.service.base.result.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.atguigu.guli.edu.dao.EduTeacherDao;
import com.atguigu.guli.edu.service.EduTeacherService;
import org.springframework.util.StringUtils;


@Service("eduTeacherService")
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherDao, Teacher> implements EduTeacherService {

    @Autowired
    EduTeacherDao eduTeacherDao;

    @Autowired
    OssFeign ossFeign;

    @Override
    public PageUtils queryPage(Integer page, Integer limit, TeacherQuery teacherQuery) {
        Page<Teacher> teacherPage = new Page<>(page, limit);
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        String beginDate = teacherQuery.getBegin();
        String endDate = teacherQuery.getEnd();
        Integer level = teacherQuery.getLevel();
        String name = teacherQuery.getName();

        if (!StringUtils.isEmpty(name)){
            queryWrapper.likeRight("name",name);
        }
        if (level!=null){
            queryWrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(beginDate)){
            queryWrapper.ge("join_date",beginDate);
        }
        if (!StringUtils.isEmpty(endDate)){
            queryWrapper.le("join_date",endDate);
        }
        baseMapper.selectPage(teacherPage, queryWrapper);
        return new PageUtils(teacherPage);
    }

    /**
     * 逻辑删除讲师id
     * @param id
     */
    @Override
    public void deleteId(String id) {
        //首先删除讲师图片
        Teacher teacher = eduTeacherDao.selectById(id);
        String avatar = teacher.getAvatar();
        R avatar1 = ossFeign.deleteImage(avatar, "avatar");
        eduTeacherDao.deleteById(id);
    }

    /**
     * 分页条件查询
     * @param
     * @param
     * @return
     */
    @Override
    public PageUtils pageSelect(Map<String, Object> parms) {

        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        //封装查询信息
        String name = (String) parms.get("name");
        if (!StringUtils.isEmpty(name)){
            wrapper.and(wpp->{
                wpp.eq("name",name).or().like("name",name);
            });
        }

        String level = (String) parms.get("level");
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }

        //开始时间
        String begin = (String) parms.get("gmt_create");
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        //结束时间
        String end = (String) parms.get("gmt_modified");
        if (!StringUtils.isEmpty(level)){
            wrapper.le("gmt_modified",end);
        }

        IPage<Teacher> page = this.page(
                new Query<Teacher>().getPage(parms),
                wrapper
        );

        PageUtils pageUtils = new PageUtils(page);

        return pageUtils;
    }

    /**
     * 添加讲师
     * @param
     */
    @Override
    public void addTeacher(Teacher eduTeacherEntity) {
        this.save(eduTeacherEntity);
    }

}